﻿using AppMachine.Domain.LogicCom;
using AppMachine.EntityFrameworkCore.Repositories.LogicCom;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AppMachine.Controls.ValidationRules {
    /// <summary>
    /// 验证应用组件标签名称是否重复
    /// </summary>
    public class ValidationRuleComTagName : ValidationRule {
        /// <summary>
        /// 组件ID
        /// </summary>
        public string ComID { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            if (value != null) {
                AppCom? tempAppCom = AppComRepository.GetInstance().GetAll<AppCom>().FirstOrDefault(it => string.Equals(value.ToString(), it.ComConfig.TagName, StringComparison.OrdinalIgnoreCase));
                if (tempAppCom != null && !tempAppCom.ComID.Equals(this.ComID)) {
                    return new ValidationResult(false, $"已经存在标签组件({tempAppCom.ComConfig.TagName}:{tempAppCom.GetType().FullName})");
                }
            }

            return ValidationResult.ValidResult;
        }
    }
}
